<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="客户编码" prop="customerNo">
        <el-input v-model="formData.customerNo" placeholder="请输入客户编码" />
      </el-form-item>
      <el-form-item label="客户类型(个人、企业、个体工商户)" prop="customerType">
        <el-radio-group v-model="formData.customerType">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="客户名称" prop="customerName">
        <el-input v-model="formData.customerName" placeholder="请输入客户名称" />
      </el-form-item>
      <el-form-item label="客户简称" prop="shortName">
        <el-input v-model="formData.shortName" placeholder="请输入客户简称" />
      </el-form-item>
      <el-form-item label="客户统一信用代码" prop="customerUscc">
        <el-input v-model="formData.customerUscc" placeholder="请输入客户统一信用代码" />
      </el-form-item>
      <el-form-item label="法人" prop="legalPerson">
        <el-input v-model="formData.legalPerson" placeholder="请输入法人" />
      </el-form-item>
      <el-form-item label="法人身份证" prop="legalIdCardNo">
        <el-input v-model="formData.legalIdCardNo" placeholder="请输入法人身份证" />
      </el-form-item>
      <el-form-item label="经营范围" prop="busiScope">
        <el-input v-model="formData.busiScope" placeholder="请输入经营范围" />
      </el-form-item>
      <el-form-item label="联系人" prop="contacts">
        <el-input v-model="formData.contacts" placeholder="请输入联系人" />
      </el-form-item>
      <el-form-item label="联系电话" prop="contactsPhone">
        <el-input v-model="formData.contactsPhone" placeholder="请输入联系电话" />
      </el-form-item>
      <el-form-item label="固定电话" prop="fixedPhone">
        <el-input v-model="formData.fixedPhone" placeholder="请输入固定电话" />
      </el-form-item>
      <el-form-item label="客户地区划代码" prop="customerAddrCode">
        <el-input v-model="formData.customerAddrCode" placeholder="请输入客户地区划代码" />
      </el-form-item>
      <el-form-item label="所在城市" prop="customerAddr">
        <el-input v-model="formData.customerAddr" placeholder="请输入所在城市" />
      </el-form-item>
      <el-form-item label="详细地址" prop="customerAddrDetail">
        <el-input v-model="formData.customerAddrDetail" placeholder="请输入详细地址" />
      </el-form-item>
      <el-form-item label="注册地址" prop="registerAddr">
        <el-input v-model="formData.registerAddr" placeholder="请输入注册地址" />
      </el-form-item>
      <el-form-item label="收件地址" prop="receiveAddr">
        <el-input v-model="formData.receiveAddr" placeholder="请输入收件地址" />
      </el-form-item>
      <el-form-item label="付款方式" prop="paymentMethod">
        <el-input v-model="formData.paymentMethod" placeholder="请输入付款方式" />
      </el-form-item>
      <el-form-item label="结算类型" prop="settlementType">
        <el-select v-model="formData.settlementType" placeholder="请选择结算类型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="结算方式" prop="settlementWays">
        <el-input v-model="formData.settlementWays" placeholder="请输入结算方式" />
      </el-form-item>
      <el-form-item label="结算天数" prop="settlementDays">
        <el-input v-model="formData.settlementDays" placeholder="请输入结算天数" />
      </el-form-item>
      <el-form-item label="回单类型" prop="receiptType">
        <el-select v-model="formData.receiptType" placeholder="请选择回单类型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="机构ID" prop="deptId">
        <el-input v-model="formData.deptId" placeholder="请输入机构ID" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { CustomerApi, CustomerVO } from '@/api/base/customer'

/** 客户管理 表单 */
defineOptions({ name: 'CustomerForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  customerNo: undefined,
  customerType: undefined,
  customerName: undefined,
  shortName: undefined,
  customerUscc: undefined,
  legalPerson: undefined,
  legalIdCardNo: undefined,
  busiScope: undefined,
  contacts: undefined,
  contactsPhone: undefined,
  fixedPhone: undefined,
  customerAddrCode: undefined,
  customerAddr: undefined,
  customerAddrDetail: undefined,
  registerAddr: undefined,
  receiveAddr: undefined,
  paymentMethod: undefined,
  settlementType: undefined,
  settlementWays: undefined,
  settlementDays: undefined,
  receiptType: undefined,
  deptId: undefined
})
const formRules = reactive({
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await CustomerApi.getCustomer(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as CustomerVO
    if (formType.value === 'create') {
      await CustomerApi.createCustomer(data)
      message.success(t('common.createSuccess'))
    } else {
      await CustomerApi.updateCustomer(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    customerNo: undefined,
    customerType: undefined,
    customerName: undefined,
    shortName: undefined,
    customerUscc: undefined,
    legalPerson: undefined,
    legalIdCardNo: undefined,
    busiScope: undefined,
    contacts: undefined,
    contactsPhone: undefined,
    fixedPhone: undefined,
    customerAddrCode: undefined,
    customerAddr: undefined,
    customerAddrDetail: undefined,
    registerAddr: undefined,
    receiveAddr: undefined,
    paymentMethod: undefined,
    settlementType: undefined,
    settlementWays: undefined,
    settlementDays: undefined,
    receiptType: undefined,
    deptId: undefined
  }
  formRef.value?.resetFields()
}
</script>
